using Microsoft.Practices.ObjectBuilder2;

namespace Common.Unity.Extensions
{
	public static class IPolicyListExtensions
	{
		public static BuilderPolicyInterface GetDefaultEnsured<BuilderPolicyInterface,BuilderPolicyType>( this IPolicyList policies ) where BuilderPolicyInterface : IBuilderPolicy where BuilderPolicyType : class, BuilderPolicyInterface, new()
		{
			return policies.GetEnsured<BuilderPolicyInterface,BuilderPolicyType>( null );
		}

			public static BuilderPolicyInterface GetEnsured<BuilderPolicyInterface,BuilderPolicyType>( this IPolicyList policies, object buildKey ) where BuilderPolicyInterface : IBuilderPolicy where BuilderPolicyType : class, BuilderPolicyInterface, new()
		{
			BuilderPolicyInterface current = policies.Get<BuilderPolicyInterface>( buildKey );
			if ( current == null )
			{
				policies.Set( current = new BuilderPolicyType(), buildKey );
			}
			return current;
		}
	}
}